﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
using Hammock;
using Newtonsoft.Json;
using Hammock.Streaming;

namespace TwitterLib
{
    public class Sinaurl_Info
    {
        private string shortUrl;
        private JObject _jobject;
        private string _url;
        private string _title;
        private string _type;
        private string _screen;
        private string _flash;
        private string _icon;
        private string _author;

        public string url
        {
            get { return _url; }
            set { _url = value; }
        }
        public string title
        {
            get { return _title; }
            set { _title = value; }
        }
        public string type
        {
            get { return _type; }
            set { _type = value; }
        }
        public string screen
        {
            get { return _screen; }
            set { _screen = value; }
        }
        public string flash
        {
            get { return _flash; }
            set { _flash = value; }
        }
        public string icon
        {
            get { return _icon; }
            set { _icon = value; }
        }
        public string author
        {
            get { return _icon; }
            set { _icon = value; }
        }

        public static bool IsVideo(string _url)
        {
            if (new Sinaurl_Info(_url).type == "video")
            {
                return true;
            }
            else
            {
                return false;
            } 
        }

        public string ShortUrl
        {
            get {
                if (shortUrl != null)
                {
                    return shortUrl;
                }
                else
                {
                    if (url != null)
                    {
                        return url.Remove(0, url.LastIndexOf('/') + 1);
                    }
                    return shortUrl;
                }
            }
            set { shortUrl = value; }
        }
        public Sinaurl_Info(JObject jo)
        {
            _jobject = jo;
            url = jo["url"].ToString().Trim('"');
            title = jo["title"].ToString().Trim('"');
            type = jo["type"].ToString().Trim('"');
            screen = jo["screen"].ToString().Trim('"');
            flash = jo["flash"].ToString().Trim('"');
            icon = jo["icon"].ToString().Trim('"');
        }
        public Sinaurl_Info(string _url)
        {
            url = _url;
            shortUrl=url.Remove(0,url.LastIndexOf('/')+1);
            client_Request(shortUrl);
        }


        private void client_Request(string shortUrl)
        {
            try
            {
                RestClient client = new RestClient();
                RestRequest request = new RestRequest
                {
                    Path = "http://t.sina.com.cn/mblog/sinaurl_info.php"
                };
                request.AddParameter("url", ShortUrl);
                RestResponse response = client.Request(request);
                JObject jo =
                    (JObject)((JObject)(JsonConvert.DeserializeObject(response.Content)))["data"][shortUrl];
                if (jo["type"].ToString().Trim('"') == "video")
                {
                    _jobject = jo;
                    url = jo["url"].ToString().Trim('"');
                    title = jo["title"].ToString().Trim('"');
                    type = jo["type"].ToString().Trim('"');
                    screen = jo["screen"].ToString().Trim('"');
                    flash = jo["flash"].ToString().Trim('"');
                    icon = jo["icon"].ToString().Trim('"');
                }
                if (jo["type"].ToString().Trim('"') == "music")
                {
                    _jobject = jo;
                    url = jo["url"].ToString().Trim('"');
                    title = jo["title"].ToString().Trim('"');
                    type = jo["type"].ToString().Trim('"');
                    screen = jo["screen"].ToString().Trim('"');
                    author = jo["author"].ToString().Trim('"');
                }
            }
            catch
            {
            }
        }
    }
}
